package com.yanqu.road.server.gameplayer.module.player.beauty;

import com.yanqu.road.entity.config.beauty.BeautyInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.MusicRoomConfigMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

public class MusicRoomAddPatrons extends BeautyUpgradeSkillAction {

    public MusicRoomAddPatrons(int beautyId, int position, int skillType, int batch, BeautyModule beautyModule, GamePlayer player) {
        super(beautyId, position, skillType, batch, beautyModule, player);
    }

    @Override
    public void setOldLv() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.MusicRoom.getValue())){
            this.ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        int skillId = position;
        SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
        if (skillInfo == null) {
            this.ret = GameErrorCode.E_BEAUTY_PATRONS_SKILL_ERR;
            return;
        }
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        oldLv = userBeauty.getMusicRoomSkillLevel(skillInfo.getId());
        if (oldLv == 0){
            this.ret = GameErrorCode.E_MUSIC_ROOM_PATRONS_SKILL_NO_UNLOCK;
            return;
        }
    }

    @Override
    public SkillInfo getSkillInfo() {
        int skillId = position;
        return SkillMgr.getSkillInfo(skillId);
    }

    @Override
    public void calConsumer() {
        BeautyInfo beautyInfo = BeautyMgr.getBeautyInfo(beautyId);

        int occupation = beautyInfo.getOccupation();
        int costId = MusicRoomConfigMgr.getBeautySkillLvUpCostId(occupation);

        SkillInfo skillInfo = getSkillInfo();
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        if(skillInfo != null){
            if (userBeauty.getMusicRoomSkillLevel(skillInfo.getId()) >= skillInfo.getMaxLevel()) {
                this.ret = GameErrorCode.E_BEAUTY_LEVEL_MAX;
                return;
            }

            int upAble = Math.min(this.batch, skillInfo.getMaxLevel() - userBeauty.getMusicRoomSkillLevel(skillInfo.getId()));

            //计算消耗
            long totalNeed = 0;
            for (int i = 0; i < upAble; i++) {
                //当前等级需要的消耗
                SkillUpgradeInfo consume = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), this.oldLv + i);
                if(consume == null){
                    break;
                }
                if(!player.getModule(CurrencyModule.class).currencyIsEnough(new Property(costId,totalNeed + consume.getConsumeCount()))){
                    //道具不足
                    break;
                }else {
                    totalNeed += consume.getConsumeCount();
                    this.realBatch++;
                }
            }

            this.needConsume = totalNeed;
            if(this.needConsume > 0){
                this.consumer = new Property(costId, BigInteger.valueOf(this.needConsume));
            }
            if(this.realBatch <= 0){
                this.ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            }
        }

    }

    @Override
    public void upgrade() {

        preValidArgument();

        if(this.ret == 0) {
            checkMaxLv();
        }

        if(this.ret == 0){
            calConsumer();
        }
        if(this.ret == 0) {
            SkillInfo skillInfo = getSkillInfo();
            UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
            if (!player.getModule(CurrencyModule.class).removeCurrency(consumer, eLogMoneyType.Beauty, eLogMoneyType.BeautyMusicRoomSkillUpgrade)) {
                this.ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;          //升级道具不足
            }else {
                userBeauty.addMusicRoomSkillLevel(skillInfo.getId(),  this.realBatch);
                beautyModule.checkBeautyMusicRoomSkill(userBeauty);
            }
        }
    }
}
